package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.JxcProductOrderService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-12-18
 */
@RestController
@RequestMapping("/do/productOrder")
public class JxcProductOrderController {

    @Resource
    JxcProductOrderService jxcProductOrderService;
    @PostMapping(value = "edit")//单据提交功能
    public Map<String, Object> edit(JxcProductOrder jxcProductOrder) {
        //非采购入库与销售出库，其他人无法操作
        if((2 == jxcProductOrder.getOrderType() && SecurityUtils.getSubject().hasRole("sale"))
                || (1 == jxcProductOrder.getOrderType() && SecurityUtils.getSubject().hasRole("purchase"))){
            return jxcProductOrderService.edit(jxcProductOrder);
        }else{
            return Result.failJsonResp("权限不足");
        }
    }
    @RequestMapping(value="findOnWayByParams" )//单据审核查询
    public Map<String, Object>  findOnWayByParams(InputParam inputParam) throws Exception {

        inputParam.setStatus(1);//在途单
        Page<JxcProductOrder> result = jxcProductOrderService.findPage(inputParam);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }
    @RequestMapping(value="findUserUnFinishedByParams" )//在途单与驳回单查询
    public Map<String, Object>  findUserUnFinishedByParams(InputParam inputParam) throws Exception {
        User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        inputParam.setCreaterId(user.getId());
        inputParam.setFinished(false);
        Page<JxcProductOrder> result = jxcProductOrderService.findPage(inputParam);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }
    @PostMapping(value="operateOrder" )//单据审核通过与驳回
    public Map<String, Object> operateOrder(boolean agreeFlag,String orderId,String remark) throws Exception {

        try {
            return jxcProductOrderService.operateOrder(agreeFlag, orderId,remark);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failJsonResp("操作失败");
        }
    }
    @PostMapping(value="deleteOrder" )//撤销单据
    public Map<String, Object> deleteOrder(String id) throws Exception {

        try {
             jxcProductOrderService.deleteById(id);
             return Result.sucJsonResp(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failJsonResp("操作失败");
        }
    }

    @PostMapping(value="modifyOrder" )//驳回单的重新提交
    public Map<String, Object> modifyOrder(JxcProductOrder po) throws Exception {

        try {
            po.setStatus(1);//修改状态
            jxcProductOrderService.insertOrUpdate(po);
            return  Result.sucJsonResp(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failJsonResp("操作失败");
        }
    }
}

